package com.deeper.customview.ui.c2.demo.c02d02;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class CoordinateView extends View {
    public CoordinateView(Context context) {
        super(context);
    }


    public CoordinateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    public CoordinateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.STROKE);

        //保存现场
        canvas.save();
        for (int i = 0; i < 10; i++) {
            canvas.drawRect(0, 0, 150, 150, paint);
            canvas.translate(30, 30);
        }
        canvas.restore();

        //平移坐标，让接下来的绘图在上此次的图形下面
        canvas.translate(0, 500);
        canvas.save();
        for (int i = 0; i < 10; i++) {
            canvas.drawRect(0, 0, 300, 300, paint);
            canvas.scale(0.9f, 0.9f, 150, 150);
        }
        canvas.restore();

        canvas.translate(0, 500);
        canvas.save();
        canvas.drawCircle(150, 150, 150, paint);
        for (int i = 0; i < 12; i++) {
            canvas.drawLine(280, 150, 300, 150, paint);
            canvas.rotate(30, 150, 150);
        }
        canvas.restore();
    }
}
